@layout SettingsLayout
@page "/admin/settings/scripts/"
@inject HttpClient _http
@inject IToaster _toaster
@inject IStringLocalizer<Resource> _localizer

<PageTitleComponent Title="@_localizer["script-settings"]" />

<h1 class="section-title">@_localizer["script-settings"]</h1>
<p class="section-desc">@_localizer["include-scripts"]</p>
<div class="section-content">
  @if (Blog != null)
  {
    <EditForm Model="@Blog" OnValidSubmit="Save">
      <div class="form-item">
        <label class="form-label">@_localizer["script-header"]</label>
        <textarea class="form-control" rows="7" @bind="Blog.HeaderScript" name="headerScripts" />
      </div>
      <div class="form-item">
        <label class="form-label">@_localizer["script-footer"]</label>
        <textarea class="form-control" rows="7" @bind="Blog.FooterScript" name="footerScripts" />
      </div>
      <div class="form-item">
        <button class="btn btn-blogifier px-5" type="submit">@_localizer["save"]</button>
      </div>
    </EditForm>
  }
</div>


@code {
  protected BlogEitorDto? Blog;

  protected override async Task OnInitializedAsync()
  {
    Blog = await _http.GetFromJsonAsync<BlogEitorDto>("api/blog");
  }

  protected async Task Save()
  {
    Toast(await _http.PutAsJsonAsync<BlogEitorDto>("api/blog", Blog!));
  }

  protected void Toast(HttpResponseMessage msg)
  {
    if (msg.IsSuccessStatusCode)
      _toaster.Success(_localizer["completed"]);
    else
      _toaster.Error(_localizer["generic-error"]);
  }
}
